/**----------------------------------------
 * @description 坐标工具类
 * @author      lxc
 * @file        PosTool.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */


export class PosTool {
    /**
     * 将 native(原点为左上角) 坐标转换为世界坐标
     * @param nativePos 
     * @returns 
     */
    turnNativePosToWorld(nativePos: cc.Vec2): cc.Vec2 {
        if (nativePos.x > 0 && nativePos.y > 0) {
            let visibleSize = cc.view.getVisibleSize()
            let desSize = cc.view.getDesignResolutionSize()
            let world_x = nativePos.x * (visibleSize.width / desSize.width)
            let world_y = visibleSize.height - nativePos.y * (visibleSize.height / desSize.height)
            return cc.v2(world_x, world_y)
        }
        return null;
    }
    /**
     * a点到 b 点的单位向量
     * @description a到b 的向量 = b 点 - a 点
     * @param a 
     * @param b 
     */
    getAtoBNormalize(a: cc.Vec2, b: cc.Vec2): cc.Vec2 {
        let x = b.x - a.x;
        let y = b.y - a.y;
        return cc.v2(x, y).normalizeSelf()
    }
    /**
     * 获取矩形的四个点
     * @param rect 
     * @returns 
     */
    getRectPoints(rect: cc.Rect): cc.Vec2[] {
        let points = []
        points.push(cc.v2(rect.x, rect.y));
        points.push(cc.v2(rect.x + rect.width, rect.y));
        points.push(cc.v2(rect.x, rect.y + rect.height));
        points.push(cc.v2(rect.x + rect.width, rect.y + rect.height));
        return points;
    }
    /**
     * 判断矩形和以centerPoint为圆心半径为 radius 的圆是否相交
     * @param rect 
     * @param centerPoint 
     * @param radius 
     */
    judgeRectCircleIntersect(rect: cc.Rect, centerPoint: cc.Vec2, radius: number) {
        let points = this.getRectPoints(rect)
        return cc.Intersection.polygonCircle(points, {
            position: centerPoint,
            radius: radius,
        })
    }
}